﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Okazuki.MVVM.PrismSupport.ViewModels.Utils;
using Okazuki.MVVM.PrismSupport.ViewModels;
using Microsoft.Practices.Prism.Commands;

namespace Okazuki.MVVM.PrismSupport.Test.ViewModels.Utils
{
    [TestClass]
    public class ViewModelInitializerTest
    {
        class MockViewModel : ViewModelBase
        {
            public bool SampleExecuteCalled { get; set; }

            public bool CanSampleExecuteCalled { get; set; }

            [AutoInitCommand]
            public DelegateCommand SampleCommand { get; set; }

            [CommandMethod]
            private void SampleExecute()
            {
                this.SampleExecuteCalled = true;
            }

            [CommandMethod]
            private bool CanSampleExecute()
            {
                this.CanSampleExecuteCalled = true;
                return true;
            }

            [AutoInitCommand]
            public DelegateCommand<int?> Sample2Command { get; set; }

            [CommandMethod]
            private void Sample2Execute(int? s) { }
            [CommandMethod]
            private bool CanSample2Execute(int? s) { return true; }
        }

        [TestMethod]
        public void TestInit()
        {
            var viewModel = new MockViewModel();
            Assert.IsNotNull(viewModel.SampleCommand, "初期化されているはず");

            Assert.IsFalse(viewModel.SampleExecuteCalled, "メソッドは呼ばれていない");
            Assert.IsFalse(viewModel.CanSampleExecuteCalled, "メソッドは呼ばれていない");

            Assert.IsTrue(viewModel.SampleCommand.CanExecute(), "常にtrueを返す");
            Assert.IsTrue(viewModel.CanSampleExecuteCalled, "メソッドは呼ばれた");

            viewModel.SampleCommand.Execute();
            Assert.IsTrue(viewModel.SampleExecuteCalled, "メソッドは呼ばれた");
        }
    }
}
